import cv2 as cv
import sys
import os
from python_ai.common.xcommon import *
import matplotlib.pyplot as plt
import numpy as np

spr = 4
spc = 5
spn = 0
plt.figure(figsize=[16,16])

img_root = '../../../../large_data/pic'
img_names = ['dog.jpg', 'dog_bird.jpg', 'dog_bird.png', 'DSC05024_1.JPG', 'DSC05024_1.png', 'friends.png']
opts = [cv.IMREAD_COLOR, cv.IMREAD_GRAYSCALE, cv.IMREAD_UNCHANGED]
i = -1
for name in img_names:
    path = os.path.join(img_root, name)
    for opt in opts:
        i += 1
        title = f'#{i+1}, name: {name}, opt: {opt} by idx'
        sep(title)
        img = cv.imread(path, opt)
        if img is None:
            print('Cannot read!')
            continue
        print(img.shape)
        ndim = len(img.shape)
        if 2 == ndim:
            print(img[:3, :3])
        elif 3 == ndim:
            h, w, ch = img.shape
            print(img[:3, :3, :])
            if ch == 3:
                img = img[:, :, [2, 1, 0]]  # BGR 2 RGB
            elif ch == 4:
                img = img[:, :, [2, 1, 0, 3]]  # BGR 2 RGB
            else:
                print(f'Cannot show, ch = {ch}')
                continue
        else:
            print(f'Cannot show, ndim = {ndim}')
            continue
        spn += 1
        if spn > spr * spc:
            break
        plt.subplot(spr, spc, spn)
        plt.imshow(img)
        plt.axis('off')
        plt.title(title)
    if spn > spr * spc:
        break

plt.show()
